package com.lxy.thread;

import java.util.HashMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.lxy.data_bean.EveryDay_condition;
import com.lxy.data_bean.SixDayWeather_bean;
import com.lxy.util.MyUrl;

import android.os.Handler;
import android.os.Message;

public class Thread_SixDay extends Thread {
	private Handler h;
	private String url;
	private HashMap<String, String> week_hash;
	public Thread_SixDay(String url,Handler h){
		this.h=h;
		this.url=url;
		week_hash=new HashMap<String, String>();
		week_hash.put("星期日", "0");
		week_hash.put("星期一", "1");
		week_hash.put("星期二", "2");
		week_hash.put("星期三", "3");
		week_hash.put("星期四", "4");
		week_hash.put("星期五", "5");
		week_hash.put("星期六", "6");
		week_hash.put("0", "星期日");
		week_hash.put("1", "星期一");
		week_hash.put("2", "星期二");
		week_hash.put("3", "星期三");
		week_hash.put("4", "星期四");
		week_hash.put("5", "星期五");
		week_hash.put("6", "星期六");
		
	}
	@Override
	public void run() {
		JSONObject weatherdata=null;
		JSONObject infodata=null;
		
		String data=MyUrl.getDocumentAt(url);
//			Log.i("data0->",data);
		try {
			 weatherdata=new JSONObject(data);
			 infodata= (JSONObject)  weatherdata.get("weatherinfo");
//			 	Log.i("infodata-->",infodata.toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
		
		SixDayWeather_bean sixday=new SixDayWeather_bean();
		
		try {
			String ww=infodata.getString("weather1");
//				Log.i("ww->",ww);
			if(ww.equals("晴")){
				sixday.setBgpic_id(1);////////!!!!!!!!!!!!!
			}else if(ww.equals("多云")){
				
			}else if(ww.equals("阴")){
				
			}else if(ww.equals("雨")){
				
			}else if(ww.equals("雪")){
				
			}
			sixday.setCity(infodata.getString("city"));
			sixday.setDate(infodata.getString("date_y"));
			sixday.setId(infodata.getString("cityid"));
			sixday.setWeather_describe(infodata.getString("weather1")+" "+
						infodata.getString("wind1"));
//				Log.i("describe-->",sixday.getWeather_describe());//
			sixday.setFx1(infodata.getString("fx1"));
			sixday.setFx2(infodata.getString("fx2"));
			EveryDay_condition []evc=new EveryDay_condition[6];
			String week_s=infodata.getString("week");
//				Log.i("week_s->",week_s);//
			int week_i=Integer.parseInt(week_hash.get(week_s));
//				Log.i("week_i",Integer.toString(week_i));
				
			for(int i=0;i<6;i++){
				evc[i]=new EveryDay_condition();
				String week1=Integer.toString((week_i+i)%7);
				evc[i].setDate(week_hash.get(week1));
				String img1=infodata.getString("img"+(i*2+1));
				evc[i].setImg_id(Integer.parseInt(img1));
				String img2=infodata.getString("img"+(i*2+2));
				if(img2.equals("99")){
					evc[i].setImg_id(Integer.parseInt(img1));
				}else{
					evc[i].setImg_id(Integer.parseInt(img2));
				}
				evc[i].setTemps(infodata.getString("temp"+(i+1)));
				evc[i].setDes(infodata.getString("weather"+(i+1)));
				evc[i].setWind(infodata.getString("wind"+(i+1)));
				evc[i].setWind_level(infodata.getString("fl"+(i+1)));
				evc[i].setImg_id_des(infodata.getString("img_title"+(i*2+1)));
				evc[i].setImg_id_night_des(infodata.getString("img_title"+(i*2+2)));
			}
			sixday.setEveryDay(evc);
			sixday.setIndex1(infodata.getString("index"));   //舒适度
			sixday.setIndex2(infodata.getString("index_uv"));//紫外线
			sixday.setIndex3(infodata.getString("index_cl"));//晨练
			sixday.setIndex4(infodata.getString("index_ag"));//息斯敏
			sixday.setIndex5(infodata.getString("index_xc"));//洗车
			sixday.setIndex6(infodata.getString("index_tr"));//旅游
			sixday.setIndex7(infodata.getString("index_ls"));//晾晒
			sixday.setIndex8(infodata.getString("index_d")); //着装
			
		
		} catch (JSONException e) {
			e.printStackTrace();
		}
//		Log.i("info->",sixday.toString());
		Message msg=Message.obtain();
		msg.what=3;
		msg.obj=sixday;
		h.sendMessage(msg);
	}
	
}
